// TTLock Demo Login
import debounce from "debounce";
import * as OauthAPI from "../../api/interfaces/oauth";
import * as MSroomAPI from "../../api/interfaces/MSroom";

import { HttpHandler } from "../../api/handle/httpHandler";
import { AES_Encrypt, AES_Decrypt, MD5_Encrypt } from "../../utils/crypto";

interface FormStatus {
  username?: string; // 账号
  password?: string; // 密码
}

Page({
  data: {
    username: "admin", // 用户名
    password: "123456", // 密码
  },
  onLoad() {


  },
  handleInputEmpty() { }, // 解决绑定数据输入报错

  // 输入校验
  handleCheckInput(event: FormStatus) {
    if (!event.username || !event.password) {
      HttpHandler.showErrorMsg("请输入账号和密码");
      return false;
    } else {
      return true;
    }
  },
  claer() {
    wx.clearStorageSync()
    wx.showToast(
      {
        title: '清除成功'
      }
    )
    setTimeout(()=>{
      wx.switchTab({
        url: '/pages/hotelList/hotelList'
      })
    },500)
  
  },
  /** 提交用户登录 */
  handleSubmit: debounce(function (event) {

    const value = event.detail.value as FormStatus;
    const flag = this.handleCheckInput(value);
    if (!flag) return;
    const option = {
      "username": value.username,
      // "password": MD5_Encrypt(value.password)
      "password": value.password
    };
    MSroomAPI.classicLogin(option).then(res => {
      if (res.result) {
        wx.removeStorageSync("token");

        wx.setStorageSync("token", res.result.token);
        wx.setStorageSync("role", res.result.roleCode);
        wx.navigateTo({
          url: '/pages/manageHotel/manageHotel',
        })
      } else {
        wx.showToast(
          {
            title: res.message,
            icon: 'none'
          }
        )
      }
    })


  }, 100),
})